using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using GTA;
using Newtonsoft.Json;
using NI;
using offline2048;
using UnityEngine;

namespace offline2048
{
    public class MainManager : MonoSingleton<MainManager>
    {
        public UIShop m_UIShop;
        public UIGuide m_UIGuide;
        public GameObject m_AdError;
        public UIMain m_UIOganicShop;
        
        public Action m_PlayTopUIUpdateAction;
        public bool m_isOpenUI;

        public float m_OpenInterCMTime;
        public int m_TouchCount;
        public float m_TouchCountTime; 
            
        public void AddTestTouch()
        {
            m_TouchCountTime = 0;
            m_TouchCount++;
            if (m_TouchCount >= 30)
            {
                UserLayerManager.Instance.ChangeUserLayerByGM(UserLayerManager.UserLayerType.Install);
            }
        }

        public void ClearTestTouch()
        {
            m_TouchCount = 0;
        }
        
        public void Init()
        {
            
        }

        void Update()
        {
            m_OpenInterCMTime += Time.deltaTime;
            if (m_TouchCount > 0)
            {
                m_TouchCountTime += Time.deltaTime;
                if (m_TouchCountTime >= 3)
                {
                    m_TouchCountTime = 0;
                    ClearTestTouch();
                }
            }
            TTicker.tUpdate();
        }

        public void ShowOganicShop()
        {
            if (m_UIOganicShop == null)
            {
                m_UIOganicShop = GameObject.Find("Canvas").transform.Find("UIMainTips").GetComponent<UIMain>();
            }
            m_UIOganicShop.gameObject.SetActive(true);
        }

        public void HideOganicShop()
        {
            if (m_UIOganicShop == null)
            {
                m_UIOganicShop = GameObject.Find("Canvas").transform.Find("UIMainTips").GetComponent<UIMain>();
            }
            m_UIOganicShop.gameObject.SetActive(false);
        }
        
        public void ShowAdError()
        {
            if (m_AdError == null)
            {
                m_AdError = GameObject.Find("Canvas").transform.Find("AdError").gameObject;
            }
            m_AdError.SetActive(true);
            TTicker.AddCallback(HideAdError,0.5f);
        }

        public void HideAdError(object o)
        {
            m_AdError.SetActive(false);
        }
        
        public void ShowUIGuide()
        {
            if (m_UIGuide == null)
            {
                m_UIGuide = GameObject.Find("Canvas").transform.Find("UIGuide").GetComponent<UIGuide>();
            }
            m_UIGuide.gameObject.SetActive(true);
            m_UIGuide.ShowGuide();
        }

        public void HideUIGuide()
        {
            m_UIGuide.gameObject.SetActive(false);
        }
        
        public void ShowUIShop()
        {
            if (m_UIShop == null)
            {
                m_UIShop = GameObject.Find("Canvas").transform.Find("UIShop").GetComponent<UIShop>();
            }
            m_UIShop.gameObject.SetActive(true);   
            m_UIShop.InitData();
            m_isOpenUI = true;
        }
        
        public void HideUIShop()
        {
            EventManager2.SetShopBtnData();
            m_UIShop.gameObject.SetActive(false);
            m_isOpenUI = false;
        }
        
        //对doller进行操作
        public void AddDoller(int addDoller)
        {
            int _doller = PlayerPrefsManager.GetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT);
            _doller += addDoller;
            PlayerPrefsManager.SetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT, _doller);
            if (MainManager.Instance.m_PlayTopUIUpdateAction != null)
            {
                MainManager.Instance.m_PlayTopUIUpdateAction();
            }
            
        }
        
        public void SubDoller(int subDoller)
        {
            int _doller = PlayerPrefsManager.GetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT);
            _doller -= subDoller;
            PlayerPrefsManager.SetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT, _doller);
            if (MainManager.Instance.m_PlayTopUIUpdateAction != null)
            {
                MainManager.Instance.m_PlayTopUIUpdateAction();
            }
        }
        
        public void CheckFisrtDayWatchRewardCM()
        {
            string _saveDay = PlayerPrefsManager.GetString(PlayerPrefsManager.FISRT_DAY_WATCH_REWARD_CM_COUNT_DAY,"");
            if (_saveDay.Equals(""))
            {
                DateTime dt = GTA.Utils.TimeStampToDateUTC();
                string _todayDate = dt.ToString("yyyyMMdd");
                PlayerPrefsManager.SetString(PlayerPrefsManager.FISRT_DAY_WATCH_REWARD_CM_COUNT_DAY,_todayDate);
                AddFisrtDayWatchRewardCMCount();
            }
            else
            {
                DateTime dt = GTA.Utils.TimeStampToDateUTC();
                string _todayDate = dt.ToString("yyyyMMdd");
                if (_saveDay.Equals(_todayDate))
                {
                    //同一天
                    AddFisrtDayWatchRewardCMCount();
                    int _count = GetTodayWatchRewardCMCount();
                    if (_count == 5)
                    {
                        AdjustWrapper.Instance.LogEvent("a6ckwr", null);
                    }
                    else if (_count == 10)
                    {
                        AdjustWrapper.Instance.LogEvent("h9wehv", null);
                    }
                    else if (_count == 15)
                    {
                        AdjustWrapper.Instance.LogEvent("q9twpw", null);
                    }
                }
                else
                {
                    //nothing
                }
            }
        }
        
        private void AddFisrtDayWatchRewardCMCount()
        {
            int _count = GetTodayWatchRewardCMCount();
            _count++;
            PlayerPrefsManager.SetInt(PlayerPrefsManager.FISRT_DAY_WATCH_REWARD_CM_COUNT,_count);
        }
        
        private int GetTodayWatchRewardCMCount()
        {
            return PlayerPrefsManager.GetInt(PlayerPrefsManager.FISRT_DAY_WATCH_REWARD_CM_COUNT, 0);
        }
        
        public void CheckThreeDayWatchInterCM()
        {
            int _dayCount = PlayerPrefsManager.GetInt(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT_DAY_COUNT, 1);
            if (_dayCount <= 3)
            {
                string _saveDay = PlayerPrefsManager.GetString(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT_LAST_DAY,"");
                if (_saveDay.Equals(""))
                {
                    DateTime dt = GTA.Utils.TimeStampToDateUTC();
                    string _todayDate = dt.ToString("yyyyMMdd");
                    PlayerPrefsManager.SetString(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT_LAST_DAY,_todayDate);
                    AddThreeDayWatchInterCMCount();
                }
                else
                {
                    DateTime dt = GTA.Utils.TimeStampToDateUTC();
                    string _todayDate = dt.ToString("yyyyMMdd");
                    if (_saveDay.Equals(_todayDate))
                    {
                        //同一天
                        AddFisrtDayWatchRewardCMCount();
                        int _count = GetThreeWatchInterCMCount();
                        if (_count == 15)
                        {
                            AdjustWrapper.Instance.LogEvent("25v5wp", null);
                        }
                        else if (_count == 25)
                        {
                            AdjustWrapper.Instance.LogEvent("u7thlt", null);
                        }
                        else if (_count == 35)
                        {
                            AdjustWrapper.Instance.LogEvent("kt5iuu", null);
                        }
                    }
                    else
                    {
                        _dayCount++;
                        if (_dayCount <= 3)
                        {
                            PlayerPrefsManager.SetString(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT_LAST_DAY,_todayDate);
                            PlayerPrefsManager.SetInt(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT_DAY_COUNT, _dayCount);
                            AddThreeDayWatchInterCMCount();
                            int _count = GetThreeWatchInterCMCount();
                            if (_count == 15)
                            {
                                AdjustWrapper.Instance.LogEvent("25v5wp", null);
                            }
                            else if (_count == 25)
                            {
                                AdjustWrapper.Instance.LogEvent("u7thlt", null);
                            }
                            else if (_count == 35)
                            {
                                AdjustWrapper.Instance.LogEvent("kt5iuu", null);
                            }
                        }
                    }
                }
            }
        }
        
        private void AddThreeDayWatchInterCMCount()
        {
            int _count = GetTodayWatchRewardCMCount();
            _count++;
            PlayerPrefsManager.SetInt(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT,_count);
        }
        
        private int GetThreeWatchInterCMCount()
        {
            return PlayerPrefsManager.GetInt(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT, 0);
        }

        public float m_RewardCMTime;
        public float m_RewardCMTimeMax = 240f;
        
        public string StringToBase64Json(string input)
        {
            // 将字符串转换为 JSON 格式
            string json = JsonConvert.SerializeObject(input);
        
            // 将 JSON 字符串转换为字节数组
            byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
        
            // 将字节数组转换为 Base64 编码的字符串
            string base64String = Convert.ToBase64String(jsonBytes);
        
            return base64String;
        }
        
        public string Base64ToString(string base64String)
        {
            // 将 Base64 字符串转换为字节数组
            byte[] jsonBytes = Convert.FromBase64String(base64String);
        
            // 将字节数组转换为 JSON 字符串
            string json = Encoding.UTF8.GetString(jsonBytes);
        
            // 将 JSON 字符串转换为原始字符串
            string result = JsonConvert.DeserializeObject<string>(json);
        
            return result;
        }
        
        public void InitIapMgr()
        {
            //初始化iapmgr
            List<string> iapKeys = new List<string>();
            for (int i = 1; i <= 8; i++)
            {
                iapKeys.Add(i.ToString());
            }

            IAPMgr.Instance.Init(iapKeys);
        }
        
    }
}


